package cn.com.wangluotongxin.mall.service.impl;

import cn.com.wangluotongxin.mall.model.MallActivityInfo;
import cn.com.wangluotongxin.mall.mapper.MallActivityInfoMapper;
import cn.com.wangluotongxin.mall.rabbitmq.pojo.ActivityInfoPojo;
import cn.com.wangluotongxin.mall.service.IMallActivityInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.omg.PortableServer.POA;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网络通信-商城APP-活动信息 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-10-22
 */
@Service
public class MallActivityInfoServiceImpl extends ServiceImpl<MallActivityInfoMapper, MallActivityInfo> implements IMallActivityInfoService {

    @Override
    public void activityInfoSync(ActivityInfoPojo pojo) {
        MallActivityInfo activityInfo = this.getActivityInfo(pojo);
        if(activityInfo.getId() != null) {
            if(pojo.getLoginUser() != null) {
                activityInfo.setModifyUser(pojo.getLoginUser().getId());
                activityInfo.setModifyName(pojo.getLoginUser().getUsername());
            }
            this.updateById(activityInfo);
        } else {
            if(pojo.getLoginUser() != null) {
                activityInfo.setCreateUser(pojo.getLoginUser().getId());
                activityInfo.setCreateName(pojo.getLoginUser().getUsername());
                activityInfo.setModifyUser(pojo.getLoginUser().getId());
                activityInfo.setModifyName(pojo.getLoginUser().getUsername());
            }
            this.save(activityInfo);
        }
    }

    @Override
    public MallActivityInfo getBySource(Integer sourceId) {
        return this.getOne(new LambdaQueryWrapper<MallActivityInfo>()
                .eq(MallActivityInfo::getSourceId, sourceId)
                .last(" limit 0,1"));
    }

    private MallActivityInfo getActivityInfo(ActivityInfoPojo pojo) {
        MallActivityInfo activityInfo = new MallActivityInfo();

        MallActivityInfo source = this.getBySource(pojo.getSourceId());
        if(source != null) {
            activityInfo.setId(source.getId());
        }
        activityInfo.setSourceId(pojo.getSourceId());
        activityInfo.setActivityName(pojo.getActivityName());
        activityInfo.setActivityImage(pojo.getActivityImage());
        activityInfo.setActivitySort(pojo.getActivitySort());
        activityInfo.setIsEnabled(pojo.getIsEnabled());
        activityInfo.setPageUrl(pojo.getPageUrl());
        activityInfo.setRemark(pojo.getRemark());
        return activityInfo;
    }
}
